package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.DTO.LegalAidLawyerDTO;
import com.ruoyi.system.domain.vo.LegalAidLawyerVO;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LegalAidLawyerMapper;
import com.ruoyi.system.domain.LegalAidLawyer;
import com.ruoyi.system.service.ILegalAidLawyerService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 律师信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-09
 */
@Service
@Slf4j
public class LegalAidLawyerServiceImpl implements ILegalAidLawyerService 
{
    @Autowired
    private LegalAidLawyerMapper legalAidLawyerMapper;
    @Autowired
    private SysUserMapper sysUserMapper;


    /**
     * 查询律师信息
     * 
     * @param id 律师信息主键
     * @return 律师信息
     */
    @Override
    public LegalAidLawyerVO selectLegalAidLawyerById(Long id)
    {
        return null;
    }

    /**
     * 查询律师信息列表
     *
     * @param legalAidLawyerDTO 律师信息
     * @return 律师信息
     */
    @Override
    public List<LegalAidLawyerVO> selectLegalAidLawyerList(LegalAidLawyerDTO legalAidLawyerDTO)
    {
        log.info("legalAidLawyerDTO",legalAidLawyerDTO);
        PageHelper.startPage(legalAidLawyerDTO.getPageNum(),legalAidLawyerDTO.getPageSize());
        Page<LegalAidLawyerVO> lawyerVOPage = legalAidLawyerMapper.selectLegalAidLawyerList(legalAidLawyerDTO);
        List<LegalAidLawyerVO> volist = lawyerVOPage.getResult();
        List<LegalAidLawyerVO> list = volist.stream().map(item -> {
            if ("1".equals(item.getSex())) {
                item.setSex("男");
            } else if ("0".equals(item.getSex())) {
                item.setSex("女");
            }
            if ("yes".equals(item.getDhStatue())) {
                item.setDhStatue("在线");
            } else {
                item.setDhStatue("离线");
            }
            if ("yes".equals(item.getImageStatue())) {
                item.setImageStatue("在线");
            } else {
                item.setImageStatue("离线");
            }
            if ("yes".equals(item.getOnlineStatue())) {
                item.setOnlineStatue("在线");
            } else {
                item.setOnlineStatue("离线");
            }
            return item;
        }).collect(Collectors.toList());

        return   lawyerVOPage;
    }

    /**
     * 新增律师信息
     *
     * @param legalAidLawyerDTO 律师信息
     * @return 结果
     */
    @Override
    @Transactional
    public void insertLegalAidLawyer(LegalAidLawyerDTO legalAidLawyerDTO)
    {
      log.info("legalAidLawyerDTO="+legalAidLawyerDTO);
        /**
         * 第一种方案：先把dto里面的sys_user数据取到，插入到sys_user表里面
         * 然后再取sys_user 的id 作为user_id插入到lawyer表里面
         */
        /**
         *  第二种方案：生成一个long的唯一id  作为sys_user的主键   lawyer 的user_id
         */
        long id = System.currentTimeMillis();

        SysUser user = new SysUser();
        BeanUtil.copyProperties(legalAidLawyerDTO,user);


        user.setUserId(id);
        LegalAidLawyer legalAidLawyer = new LegalAidLawyer();
        BeanUtil.copyProperties(legalAidLawyerDTO,legalAidLawyer);
        legalAidLawyer.setUserId(id);


        sysUserMapper.insertUser(user);
        legalAidLawyerMapper.insertLegalAidLawyer(legalAidLawyer);

        //获取插入后的律师id
        Long lawyerId = legalAidLawyer.getId();

    }


    /**
     * 修改律师信息
     *
     * @param legalAidLawyerVO 律师信息
     * @return 结果
     */
    @Override
    public void updateLegalAidLawyer(LegalAidLawyerVO legalAidLawyerVO)
    {
    }

    /**
     * 批量删除律师信息
     *
     * @param ids 需要删除的律师信息主键
     * @return 结果
     */
    @Override
    public int deleteLegalAidLawyerByIds(Long[] ids)
    {
        return legalAidLawyerMapper.deleteLegalAidLawyerByIds(ids);
    }

    /**
     * 删除律师信息信息
     *
     * @param id 律师信息主键
     * @return 结果
     */
    @Override
    public int deleteLegalAidLawyerById(Long id)
    {

        return legalAidLawyerMapper.deleteLegalAidLawyerById(id);
    }

    /**
     * 根据id查询律师信息和用户公共信息
     * @param id
     * @return
     */
    public LegalAidLawyerVO selectLawyerWithUserById(Long id) {
        //1.查询律师信息
        LegalAidLawyer legalAidLawyer = legalAidLawyerMapper.selectLegalAidLawyerById(id);
        Long userId = legalAidLawyer.getUserId();
        //2.属性拷贝进VO
        LegalAidLawyerVO legalAidLawyerVO = new LegalAidLawyerVO();
        BeanUtil.copyProperties(legalAidLawyer,legalAidLawyerVO);

        //3.查询用户共=公共信息
        SysUser user = sysUserMapper.selectUserById(userId);
        String phonenumber = user.getPhonenumber();
        //4.属性拷贝进VO
        BeanUtil.copyProperties(user,legalAidLawyerVO);
        String sex = legalAidLawyerVO.getSex();
        if ("0".equals(sex)) {
            legalAidLawyerVO.setSex("女");
        }else if ("1".equals(sex)){
            legalAidLawyerVO.setSex("男");
        }


        return legalAidLawyerVO;
    }

    /**
     * 修改律师信息和用户公共信息
     * @param legalAidLawyerDTO
     */
    @Transactional
    public void updateWithUser(LegalAidLawyerDTO legalAidLawyerDTO) {
        log.info("legalAidLawyerDTO,{}",legalAidLawyerDTO);
        //1.修改律师信息
        LegalAidLawyer legalAidLawyer = new LegalAidLawyer();
        BeanUtil.copyProperties(legalAidLawyerDTO,legalAidLawyer);
        legalAidLawyerMapper.updateLegalAidLawyer(legalAidLawyer);

        //2.修改用户公共信息
        SysUser user = new SysUser();
        BeanUtil.copyProperties(legalAidLawyerDTO,user);
        //3.获取userId
        Long userId = legalAidLawyerDTO.getUserId();
        user.setUserId(userId);
        sysUserMapper.updateUser(user);



    }

    /**
     * 根据id批量删除
     * @param ids
     */
    @Transactional
    public void deleteLegalAidLawyerWithUserByIds(Long[] ids) {
    //1.删除律师信息
    legalAidLawyerMapper.deleteLegalAidLawyerByIds(ids);
     //2.删除用户公共信息
      sysUserMapper.deleteUserByIds(ids);


    }
}
